package com.ruoyi.web.controller.system;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.generator.domain.GenTable;
import com.ruoyi.generator.domain.GenTableColumn;
import com.ruoyi.system.domain.DocGenTableColumn;
import com.ruoyi.system.service.IDocGenTableColumnService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DocGenTable;
import com.ruoyi.system.service.IDocGenTableService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 代码生成业务Controller
 * 
 * @author ruoyi
 * @date 2021-10-11
 */
@RestController
@RequestMapping("/system/table")
public class DocGenTableController extends BaseController
{
    @Autowired
    private IDocGenTableService docGenTableService;

    @Autowired
    private IDocGenTableColumnService docGenTableColumnService;
    /**
     * 查询代码生成业务列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:list')")
    @GetMapping("/list")
    public TableDataInfo list(DocGenTable docGenTable)
    {
        startPage();
        List<DocGenTable> list = docGenTableService.selectDocGenTableList(docGenTable);
        return getDataTable(list);
    }

    /**
     * 导出代码生成业务列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:export')")
    @Log(title = "代码生成业务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DocGenTable docGenTable)
    {
        List<DocGenTable> list = docGenTableService.selectDocGenTableList(docGenTable);
        ExcelUtil<DocGenTable> util = new ExcelUtil<DocGenTable>(DocGenTable.class);
        return util.exportExcel(list, "代码生成业务数据");
    }

    /**
     * 导入表结构（保存）
     */
    @PreAuthorize("@ss.hasPermi('system:table:import')")
    @Log(title = "代码生成业务", businessType = BusinessType.IMPORT)
    @PostMapping("/importTable")
    public AjaxResult importTableSave(String tables)
    {
        String[] tableNames = Convert.toStrArray(tables);
        // 查询表信息
        List<DocGenTable> tableList = docGenTableService.selectDbTableListByNames(tableNames);
        docGenTableService.importDocGenTable(tableList);
        return AjaxResult.success();
    }


    /**
     * 修改保存代码生成业务*/

    @PreAuthorize("@ss.hasPermi('system:table:edit')")
    @Log(title = "代码生成业务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editSave(@Validated @RequestBody DocGenTable docGenTable)
    {
        docGenTableService.validateEdit(docGenTable);
        docGenTableService.updateDocGenTable(docGenTable);
        return AjaxResult.success();
    }

    /**
     * 修改代码生成业务
     */
    @PreAuthorize("@ss.hasPermi('system:table:edit')")
    @Log(title = "代码生成业务", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody DocGenTable docGenTable)
    {
        return toAjax(docGenTableService.updateDocGenTable(docGenTable));
    }


    /**
     * 获取代码生成业务详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:table:query')")
    @GetMapping(value = "/{tableId}")
    public AjaxResult getInfo(@PathVariable("tableId") Long tableId)
    {
        return AjaxResult.success(docGenTableService.selectDocGenTableByTableId(tableId));
    }

    /**
     * 新增代码生成业务
     */
    @PreAuthorize("@ss.hasPermi('system:table:add')")
    @Log(title = "代码生成业务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody DocGenTable docGenTable)
    {
        return toAjax(docGenTableService.insertDocGenTable(docGenTable));
    }



    /**
     * 删除代码生成业务
     */
    @PreAuthorize("@ss.hasPermi('system:table:remove')")
    @Log(title = "代码生成业务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tableIds}")
    public AjaxResult remove(@PathVariable Long[] tableIds)
    {
        return toAjax(docGenTableService.deleteDocGenTableByTableIds(tableIds));
    }

    /**
     * 查询数据库列表*/
    @PreAuthorize("@ss.hasPermi('system:table:list')")
    @GetMapping("/db/list")
    public TableDataInfo dataList(DocGenTable docGenTable)
    {
        startPage();
        List<DocGenTable> list = docGenTableService.selectDbTableList(docGenTable);
        return getDataTable(list);
    }

    /**
     * 查询数据表字段列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:list')")
    @GetMapping(value = "/column/{tableId}")
    public TableDataInfo columnList(Long tableId)
    {
        TableDataInfo dataInfo = new TableDataInfo();
        List<DocGenTableColumn> list = docGenTableColumnService.selectDocGenTableColumnListByTableId(tableId);
        dataInfo.setRows(list);
        dataInfo.setTotal(list.size());
        return dataInfo;
    }

    /**
     * 同步数据库
     */
    @PreAuthorize("@ss.hasPermi('system:table:edit')")
    @Log(title = "代码生成", businessType = BusinessType.UPDATE)
    @GetMapping("/synchDb/{tableName}")
    public AjaxResult synchDb(@PathVariable("tableName") String tableName)
    {
        docGenTableService.synchDb(tableName);
        return AjaxResult.success();
    }
    /**
     * 导出word前置方法
     */
    private static Configuration configuration = null;
    public File createDocFile(String templateFilePath, Map<String, Object> dataMap, String exportFilePath, int loadType) throws Exception {
        Template t = null;
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_21);
        configuration.setDefaultEncoding("UTF-8");
        try {
            templateFilePath = pathReplace(templateFilePath);
            String ftlPath = templateFilePath.substring(0, templateFilePath.lastIndexOf("/"));
            if(loadType == 1) {
                configuration.setDirectoryForTemplateLoading(new File(ftlPath)); // FTL文件所存在的位置
            }else {
                configuration.setClassForTemplateLoading(this.getClass(), ftlPath);//以类加载的方式查找模版文件路径
            }


            String ftlFile = templateFilePath.substring(templateFilePath.lastIndexOf("/")+1);
            t = configuration.getTemplate(ftlFile); // 模板文件名

            File outFile = new File(exportFilePath);
            Writer out = null;

            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));

            t.process(dataMap, out);
        } catch (Exception e) {

            throw e;
        }

        return null;
    }
    private String pathReplace(String path) {
        while(path != null && path.contains("\\")) {
            path = path.replace("\\", "/");
        }
        return path;
    }

    /**
     * 导出word列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:word')")
    @Log(title = "代码生成业务", businessType = BusinessType.EXPORT)
    @GetMapping("/word")
    public AjaxResult word(DocGenTable docGenTable)
    {
        List<DocGenTable> list = docGenTableService.selectDocGenTableList(docGenTable);
        Map<String, Object> dataMap = new HashMap<String, Object>();
        StringBuffer table = new StringBuffer();
        String tableheard ="  <w:tr>\n" +
                "                    <w:tblPrEx>\n" +
                "                        <w:tblBorders>\n" +
                "                            <w:top w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:left w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:bottom w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:right w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:insideH w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:insideV w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                        </w:tblBorders>\n" +
                "                        <w:tblCellMar>\n" +
                "                            <w:top w:w=\"0\" w:type=\"dxa\"/>\n" +
                "                            <w:left w:w=\"108\" w:type=\"dxa\"/>\n" +
                "                            <w:bottom w:w=\"0\" w:type=\"dxa\"/>\n" +
                "                            <w:right w:w=\"108\" w:type=\"dxa\"/>\n" +
                "                        </w:tblCellMar>\n" +
                "                    </w:tblPrEx>\n" +
                "                    <w:trPr/>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"1728\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"auto\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:ascii=\"宋体\" w:h-ansi=\"宋体\" w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"2700\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"auto\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:pStyle w:val=\"a23\"/>\n" +
                "                                <w:pBdr>\n" +
                "                                    <w:bottom w:val=\"nil\"/>\n" +
                "                                </w:pBdr>\n" +
                "                                <w:snapToGrid/>\n" +
                "                                <w:jc w:val=\"both\"/>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                    <w:sz w:val=\"21\"/>\n" +
                "                                    <w:sz-cs w:val=\"24\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                    <w:sz w:val=\"21\"/>\n" +
                "                                    <w:sz-cs w:val=\"24\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>";
        String tableboday="</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"4094\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"auto\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:ascii=\"宋体\" w:h-ansi=\"宋体\" w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:ascii=\"宋体\" w:h-ansi=\"宋体\" w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>";
        String tablefoot="</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                </w:tr>";
        String comment2 ="</w:p>\n" +
                "            <w:p>\n" +
                "                <w:pPr>\n" +
                "                    <w:rPr>\n" +
                "                        <w:rFonts w:ascii=\"宋体\" w:h-ansi=\"宋体\" w:hint=\"fareast\"/>\n" +
                "                        <w:sz w:val=\"24\"/>\n" +
                "                    </w:rPr>\n" +
                "                </w:pPr>\n" +
                "            </w:p>\n" +
                "            <w:tbl>\n" +
                "                <w:tblPr>\n" +
                "                    <w:tblW w:w=\"8439\" w:type=\"dxa\"/>\n" +
                "                    <w:tblInd w:w=\"108\" w:type=\"dxa\"/>\n" +
                "                    <w:tblBorders>\n" +
                "                        <w:top w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                        <w:left w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                        <w:bottom w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                        <w:right w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                        <w:insideH w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                        <w:insideV w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                    </w:tblBorders>\n" +
                "                    <w:tblLayout w:type=\"Fixed\"/>\n" +
                "                    <w:tblCellMar>\n" +
                "                        <w:top w:w=\"0\" w:type=\"dxa\"/>\n" +
                "                        <w:left w:w=\"108\" w:type=\"dxa\"/>\n" +
                "                        <w:bottom w:w=\"0\" w:type=\"dxa\"/>\n" +
                "                        <w:right w:w=\"108\" w:type=\"dxa\"/>\n" +
                "                    </w:tblCellMar>\n" +
                "                </w:tblPr>\n" +
                "                <w:tblGrid>\n" +
                "                    <w:gridCol w:w=\"1444\"/>\n" +
                "                    <w:gridCol w:w=\"737\"/>\n" +
                "                    <w:gridCol w:w=\"2332\"/>\n" +
                "                    <w:gridCol w:w=\"1083\"/>\n" +
                "                    <w:gridCol w:w=\"722\"/>\n" +
                "                    <w:gridCol w:w=\"2121\"/>\n" +
                "                </w:tblGrid>\n" +
                "                <w:tr>\n" +
                "                    <w:tblPrEx>\n" +
                "                        <w:tblBorders>\n" +
                "                            <w:top w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:left w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:bottom w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:right w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:insideH w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:insideV w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                        </w:tblBorders>\n" +
                "                        <w:tblCellMar>\n" +
                "                            <w:top w:w=\"0\" w:type=\"dxa\"/>\n" +
                "                            <w:left w:w=\"108\" w:type=\"dxa\"/>\n" +
                "                            <w:bottom w:w=\"0\" w:type=\"dxa\"/>\n" +
                "                            <w:right w:w=\"108\" w:type=\"dxa\"/>\n" +
                "                        </w:tblCellMar>\n" +
                "                    </w:tblPrEx>\n" +
                "                    <w:trPr>\n" +
                "                        <w:cantSplit w:val=\"on\"/>\n" +
                "                        <w:trHeight w:val=\"361\" w:h-rule=\"atLeast\"/>\n" +
                "                    </w:trPr>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"1444\" w:type=\"dxa\"/>\n" +
                "                            <w:tcBorders>\n" +
                "                                <w:bottom w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            </w:tcBorders>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"D9D9D9\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:pStyle w:val=\"a23\"/>\n" +
                "                                <w:pBdr>\n" +
                "                                    <w:bottom w:val=\"nil\"/>\n" +
                "                                </w:pBdr>\n" +
                "                                <w:snapToGrid/>\n" +
                "                                <w:ind w:first-line=\"420\"/>\n" +
                "                                <w:jc w:val=\"both\"/>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                    <w:sz w:val=\"21\"/>\n" +
                "                                    <w:sz-cs w:val=\"24\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                    <w:sz w:val=\"21\"/>\n" +
                "                                    <w:sz-cs w:val=\"24\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>表 名</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"6995\" w:type=\"dxa\"/>\n" +
                "                            <w:gridSpan w:val=\"5\"/>\n" +
                "                            <w:tcBorders>\n" +
                "                                <w:bottom w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            </w:tcBorders>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"auto\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:fareast=\"宋体\" w:hint=\"default\"/>\n" +
                "                                    <w:lang w:val=\"EN-US\" w:fareast=\"ZH-CN\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                    <w:sz w:val=\"21\"/>\n" +
                "                                    <w:sz-cs w:val=\"24\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>";

        String comment3 = "</w:t>\n" +
                "                            </w:r>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                    <w:sz w:val=\"21\"/>\n" +
                "                                    <w:sz-cs w:val=\"24\"/>\n" +
                "                                    <w:lang w:val=\"EN-US\" w:fareast=\"ZH-CN\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>";
        String comment4 ="</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                </w:tr> <w:tr>\n" +
                "                    <w:tblPrEx>\n" +
                "                        <w:tblBorders>\n" +
                "                            <w:top w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:left w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:bottom w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:right w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:insideH w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:insideV w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                        </w:tblBorders>\n" +
                "                        <w:tblCellMar>\n" +
                "                            <w:top w:w=\"0\" w:type=\"dxa\"/>\n" +
                "                            <w:left w:w=\"108\" w:type=\"dxa\"/>\n" +
                "                            <w:bottom w:w=\"0\" w:type=\"dxa\"/>\n" +
                "                            <w:right w:w=\"108\" w:type=\"dxa\"/>\n" +
                "                        </w:tblCellMar>\n" +
                "                    </w:tblPrEx>\n" +
                "                    <w:trPr>\n" +
                "                        <w:trHeight w:val=\"361\" w:h-rule=\"atLeast\"/>\n" +
                "                    </w:trPr>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"1444\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"D9D9D9\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:jc w:val=\"center\"/>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>列名</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"737\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"D9D9D9\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:jc w:val=\"center\"/>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>描述</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"2332\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"D9D9D9\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:ind w:left=\"27\"/>\n" +
                "                                <w:jc w:val=\"center\"/>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>数据类型（精度范围）</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"1083\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"D9D9D9\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:jc w:val=\"center\"/>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>空/非空</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"722\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"D9D9D9\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:jc w:val=\"center\"/>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>唯一</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"2121\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"D9D9D9\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:jc w:val=\"center\"/>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>约束条件</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                </w:tr>";
        String cloum1 =" <w:tr>\n" +
                "                    <w:tblPrEx>\n" +
                "                        <w:tblBorders>\n" +
                "                            <w:top w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:left w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:bottom w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:right w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:insideH w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:insideV w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                        </w:tblBorders>\n" +
                "                        <w:tblCellMar>\n" +
                "                            <w:top w:w=\"0\" w:type=\"dxa\"/>\n" +
                "                            <w:left w:w=\"108\" w:type=\"dxa\"/>\n" +
                "                            <w:bottom w:w=\"0\" w:type=\"dxa\"/>\n" +
                "                            <w:right w:w=\"108\" w:type=\"dxa\"/>\n" +
                "                        </w:tblCellMar>\n" +
                "                    </w:tblPrEx>\n" +
                "                    <w:trPr>\n" +
                "                        <w:trHeight w:val=\"361\" w:h-rule=\"atLeast\"/>\n" +
                "                    </w:trPr>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"1444\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"auto\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>";
        String cloum2 = "</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"737\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"auto\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>";
        String cloum3 ="</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"2332\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"auto\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>";
        String cloum4 = "</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"1083\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"auto\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:fareast=\"宋体\" w:hint=\"fareast\"/>\n" +
                "                                    <w:lang w:val=\"EN-US\" w:fareast=\"ZH-CN\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                    <w:lang w:val=\"EN-US\" w:fareast=\"ZH-CN\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>";
        String cloum5 ="</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"722\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"auto\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:fareast=\"宋体\" w:hint=\"fareast\"/>\n" +
                "                                    <w:lang w:val=\"EN-US\" w:fareast=\"ZH-CN\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                    <w:lang w:val=\"EN-US\" w:fareast=\"ZH-CN\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>";
        String cloum6 = "</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"2121\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"auto\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                    <w:lang w:val=\"EN-US\" w:fareast=\"ZH-CN\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>无</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                </w:tr>";

        String cloumrema ="<w:tr>\n" +
                "                    <w:tblPrEx>\n" +
                "                        <w:tblBorders>\n" +
                "                            <w:top w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:left w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:bottom w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:right w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:insideH w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                            <w:insideV w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
                "                        </w:tblBorders>\n" +
                "                        <w:tblCellMar>\n" +
                "                            <w:top w:w=\"0\" w:type=\"dxa\"/>\n" +
                "                            <w:left w:w=\"108\" w:type=\"dxa\"/>\n" +
                "                            <w:bottom w:w=\"0\" w:type=\"dxa\"/>\n" +
                "                            <w:right w:w=\"108\" w:type=\"dxa\"/>\n" +
                "                        </w:tblCellMar>\n" +
                "                    </w:tblPrEx>\n" +
                "                    <w:trPr>\n" +
                "                        <w:cantSplit w:val=\"on\"/>\n" +
                "                        <w:trHeight w:val=\"723\" w:h-rule=\"atLeast\"/>\n" +
                "                    </w:trPr>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"1444\" w:type=\"dxa\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"D9D9D9\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:pStyle w:val=\"a23\"/>\n" +
                "                                <w:pBdr>\n" +
                "                                    <w:bottom w:val=\"nil\"/>\n" +
                "                                </w:pBdr>\n" +
                "                                <w:snapToGrid/>\n" +
                "                                <w:jc w:val=\"both\"/>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                    <w:sz w:val=\"21\"/>\n" +
                "                                    <w:sz-cs w:val=\"24\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                    <w:sz w:val=\"21\"/>\n" +
                "                                    <w:sz-cs w:val=\"24\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>其他说明</w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>\n" +
                "                    </w:tc>\n" +
                "                    <w:tc>\n" +
                "                        <w:tcPr>\n" +
                "                            <w:tcW w:w=\"6995\" w:type=\"dxa\"/>\n" +
                "                            <w:gridSpan w:val=\"5\"/>\n" +
                "                            <w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"auto\"/>\n" +
                "                            <w:vAlign w:val=\"top\"/>\n" +
                "                        </w:tcPr>\n" +
                "                        <w:p>\n" +
                "                            <w:pPr>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                            </w:pPr>\n" +
                "                            <w:r>\n" +
                "                                <w:t>Primary Key</w:t>\n" +
                "                            </w:r>\n" +
                "                            <w:r>\n" +
                "                                <w:rPr>\n" +
                "                                    <w:rFonts w:hint=\"fareast\"/>\n" +
                "                                </w:rPr>\n" +
                "                                <w:t>：";
        String end =" </w:t>\n" +
                "                            </w:r>\n" +
                "                        </w:p>" +
                "   </w:tc>\n" +
                "                </w:tr>\n";
        for (int j = 0;j<list.size();j++){
            String tablename = list.get(j).getTableName();
            String tablecomment = list.get(j).getTableComment();
            table.append(tableheard+tablename+tableboday+tablecomment+tablefoot);
        }

        StringBuffer comments = new StringBuffer();
        StringBuffer colu = new StringBuffer();

        for (int i = 0;i<list.size();i++){
            String commenttable = list.get(i).getTableName();
            String commentlabel = list.get(i).getTableComment();
            DocGenTableColumn docGenTableColumn = new DocGenTableColumn();
            docGenTableColumn.setTableId(list.get(i).getTableId());
            List <DocGenTableColumn> list1 =  docGenTableColumnService.selectDocGenTableColumnList(docGenTableColumn);
            for (int x = 0 ;x<list1.size();x++){
                String required="";
                String pk="";
                String name = list1.get(x).getColumnName();//获取列名称
                String column = list1.get(x).getColumnComment();//获取列描述
                String type =list1.get(x).getColumnType();//获取列类型
                if (list1.get(x).getIsRequired()==null){
                     required ="空";
                }else{
                    required ="非空";//获取是否可为空值
                }
                if (list1.get(x).getIsPk().equals("0")){
                     pk ="";//是否为主键
                }else{
                    pk ="√";
                }
                colu.append(cloum1+name+cloum2+column+cloum3+type+cloum4+required+cloum5+pk+cloum6);
            }
            String key="";//查询主键名称
           for (int z = 0; z<list1.size();z++){
               if (list1.get(z).getIsPk().equals("1")){
                   key += list1.get(z).getColumnName();
               }
           }
            comments.append(comment2+commenttable+comment3+commentlabel+comment4+colu+cloumrema+key+end);
        }



        dataMap.put("table", table);
        dataMap.put("comment", comments);
        String templateFile = "ruoyi-admin\\test.ftl";
        String exportFile = "Word\\ruoyi.doc";

        try {
            createDocFile(templateFile, dataMap, exportFile, 1);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        return AjaxResult.success("成功");
    }

}
